//
//  ViewController.swift
//  UIButtonDemo
//
//  Created by itshizhan on 2020/10/11.
//  Copyright © 2020 itshizhan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

	lazy var myBtn : MyButton = MyButton()
	
	override func viewDidLoad() {
		super.viewDidLoad()
		// Do any additional setup after loading the view.
		print("HELLO BUTTON")
		initView()
	}

	  func initView(){
		// 初始化一个按钮
		let button = UIButton(type: .custom)
		let button1 = UIButton(type: .system)
		//设置按钮位置和大小
		button.frame = CGRect(x:10, y:44, width:200, height:50)
		button1.frame = CGRect(x:10, y:100, width:100, height:50)
		view.addSubview(button)
		view.addSubview(button1)
		//设置按钮文字
		button.setTitle("普通状态", for: .normal)
		button.setTitle("高亮状态", for: .highlighted)
		button.setTitle("禁用状态", for: .disabled)
		button.setTitle("选择状态", for: .selected)
		button.setTitle("获取焦点状态", for: .focused)
		button.setTitle("禁用状态", for: .disabled)

			
			
			

		//显示文字颜色
		button.setTitleColor(UIColor.red, for: .normal)
		button.setTitleColor(UIColor.blue, for: .highlighted)
		button.setTitleColor(UIColor.cyan, for: .selected)
		button.setTitleColor(UIColor.gray, for: .disabled)
			
		//阴影文字颜色设置
		//		button.setTitleShadowColor(UIColor.cyan, for: .normal)
		//		button.setTitleShadowColor(UIColor.green, for: .highlighted)
		//		button.setTitleShadowColor(UIColor.brown, for: .disabled)
		//		button.setTitleShadowColor(UIColor.darkGray, for: .selected)
		
			
		//UIButton设置字体大小
		button.titleLabel?.font = UIFont.systemFont(ofSize: 20)
			// 是否禁用
		//button.isEnabled = false
			
		//设置边框宽度/颜色, 以及圆角
		button.layer.borderWidth = 1
		button.layer.borderColor = UIColor.gray.cgColor
		button.layer.cornerRadius = 10
			
			
			
			//按钮背景颜色
			button.backgroundColor = UIColor.lightGray
			//背景图片
			button.setBackgroundImage(UIImage(named:"pdf_bg"), for: .normal)

			
			// 按钮文字过长如何显示
			button1.setTitle("普通状态，文字很长，文字很长", for: .normal)
			button1.setTitleColor(UIColor.red, for: .normal)
			button1.titleLabel?.lineBreakMode = .byTruncatingTail
			button1.titleLabel?.numberOfLines = 2
			
			
			// 点击事件，不传参
			button.addTarget(self, action: #selector(touchUpInsideAction), for: .touchUpInside)
			button.addTarget(self, action: #selector(ViewController.touchUpInsideAction1), for: .touchUpInside)
			
			// 点击事件，传递button参数
			button.addTarget(self, action: #selector(touchUpInsideAction2(button:)), for: .touchUpInside)
			
			
			// 添加自定义Button
			myBtn.frame = CGRect(x: 0, y: 0, width: 200, height: 60)
			myBtn.setTitle("随堂练", for: .normal)
			myBtn.center = view.center
			myBtn.addTarget(self, action: #selector(ViewController.myBtnClick), for: .touchUpInside)
			view.addSubview(myBtn)
	}
	
	@objc func touchUpInsideAction() {
		print("touchUpInsideAction" )
	}
	@objc func touchUpInsideAction1() {
		print("touchUpInsideAction1" )
	}
	@objc func touchUpInsideAction2(button:UIButton){
		print("touchUpInsideAction2:" + String(button.isEnabled))
	}
	@objc func myBtnClick(){
		print("myBtnClick clicked" )
	}

}

